layui.define(['jquery','element'],function(exports){
    var $ = layui.$,
        element = layui.element;
    element.on('nav(layadmin-system-side-menu)', function(elem){
        console.log(elem[0]); //得到当前点击的DOM对象
    });
    //初始化菜单
    var consoleFn = {
        initLeftMenuLay: function (_menus) {
            var htmlPanel = [];
            $.each(_menus.menus, function (i, node) {
                var left = '';
                if (!node.children) {
                    left = 'lay-href="' + (node.menuUrl || "") + '"';
                }
                htmlPanel.push('<li class="layui-nav-item">'
                    + '<a href="javascript:;"  lay-tips="' + (node.text || node.menuName) + '" lay-direction="2" ' + left + '>'
                    + '<i class="layui-icon ' + node.menuIcon + '" ></i><cite>' + (node.text || node.menuName) + '</cite></a>');
                if (node.children && node.children.length > 0) {
                    htmlPanel.push('<dl class="layui-nav-child">');
                    $.each(node.children, function (j, child) {
                        if (child.children && child.children.length > 0) {
                            var url = (child.menuUrl) ? child.menuUrl + '?menuId=' + child.id + '&menuIcon=' + child.menuIcon + '&menuName=' + encodeURI(child.menuName) : '';
                            htmlPanel.push('<dd><a>' + (child.text || child.menuName) + '</a>');
                            htmlPanel.push('<dl class="layui-nav-child">');
                            $.each(child.children, function (j, child) {
                                if (child.menuUrl && ((child.menuUrl.indexOf("http://") == 0 || child.menuUrl.indexOf("https://") == 0) && child.menuUrl.indexOf("target=_blank") != -1)) {
                                    htmlPanel.push('<dd><a href="' + child.menuUrl + '" target="_blank">' + (child.text || child.menuName) + '</a></dd>');
                                } else {
                                    var url = (child.menuUrl) ? child.menuUrl + '?menuId=' + child.id + '&menuIcon=' + child.menuIcon + '&menuName=' + encodeURI(child.menuName) : '';
                                    htmlPanel.push('<dd><a lay-href="' + url + '">' + (child.text || child.menuName) + '</a></dd>');
                                }
                            });
                            htmlPanel.push('</dl></dd>');
                        } else {
                            if (child.menuUrl && ((child.menuUrl.indexOf("http://") == 0 || child.menuUrl.indexOf("https://") == 0) && child.menuUrl.indexOf("target=_blank") != -1)) {
                                htmlPanel.push('<dd><a href="' + child.menuUrl + '" target="_blank">' + (child.text || child.menuName) + '</a></dd>');
                            } else {
                                var url = (child.menuUrl) ? child.menuUrl + '?menuId=' + child.id + '&menuIcon=' + child.menuIcon + '&menuName=' + encodeURI(child.menuName) : '';
                                htmlPanel.push('<dd><a lay-href="' + url + '">' + (child.text || child.menuName) + '</a></dd>');
                            }
                        }
                    });
                    htmlPanel.push('</dl>');
                }
                htmlPanel.push('</li>');
                var html = htmlPanel.join('');
                $("#LAY-system-side-menu").append(html);
                htmlPanel = [];
            });
            $(".layui-nav-bar").attr("class", "zorder:999;");
            element.render('nav','layAdmin');
        }
    };

    exports('console', consoleFn);
});